﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaoYL.BoosterStation
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
            this.Load += FrmLogin_Load;
        }

        private SysAdminService adminService = new SysAdminService();

        private void FrmLogin_Load(object sender, EventArgs e)
        {
          var sysAdmin=  adminService.QuerySysAdmins();
            if (sysAdmin.Count > 0)
            {
                foreach (var item in sysAdmin)
                {
                    this.cmb_User.Items.Add(item.LoginName);
                }
                this.cmb_User.SelectedIndex = 0;
            }
            else
            {
                new MsgBoxConfirm("登陆提示","没有用户，请先添加用户！").ShowDialog();
            }
        }

        private void lbl_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Login_Click(object sender, EventArgs e)
        {
            //1.验证数据
            if (string.IsNullOrEmpty(this.cmb_User.Text.Trim()))
            {
                new MsgBoxConfirm("登陆提示", "用户名不能为空！").ShowDialog();
                return;
            }
            if (string.IsNullOrEmpty(this.text_Pwd.Text.Trim()))
            {
                new MsgBoxConfirm("登陆提示", "密码不能为空！").ShowDialog();
                return;
            }
            //2.封装用户对象
            SysAdmin sysAdmin = new SysAdmin
            {
                LoginName = this.cmb_User.Text.Trim(),
                LoginPwd = this.text_Pwd.Text.Trim()
            };
            //3.用户查询
            SysAdmin admin = adminService.AdminLogin(sysAdmin);
            //4.判断是否登录成功
            if (admin == null)
            {
                new MsgBoxConfirm("登陆提示", "用户名或密码错误！").ShowDialog();
                return;
            }
            else
            {               
                this.DialogResult = DialogResult.OK;
                Program.CurrentUser = admin;
            }
        }

        private void text_Pwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //模拟点击登录按钮
                btn_Login_Click(null, null);
            }
        }


        #region 无边框拖动 

        private Point mPoint;
        private void Panel_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void Panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }

        #endregion
    }
}
